package com.zc.auth.security.core.configuration;

import com.zc.auth.security.core.authentication.AuthenticationTokenService;
import com.zc.auth.security.core.authentication.JsonWebTokenService;
import com.zc.auth.security.core.authentication.RedisTokenService;
import com.zc.auth.security.core.authentication.JwtParseService;
import com.zc.auth.security.core.properties.JwtProperties;
import com.zc.auth.security.core.properties.SecurityProcessorProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * jwt自动装配
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/1/13 11:26
 */
@ConditionalOnMissingBean(AuthenticationTokenService.class)
@EnableConfigurationProperties({JwtProperties.class, SecurityProcessorProperties.class})
@Configuration
public class TokenConfiguration {

    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private SecurityProcessorProperties processorProperties;

    @Bean
    @ConditionalOnMissingBean(value = AuthenticationTokenService.class)
    public AuthenticationTokenService authenticationTokenService() {
        String tokenType = processorProperties.getTokenType();
        return "redis".equals(tokenType) ? new RedisTokenService() : new JwtParseService(new JsonWebTokenService().bindValues(jwtProperties));
    }

}
